﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace carApp
{
    public partial class kehuselect : Form
    {
        public kehuselect()
        {
            InitializeComponent();
        }
        public kehuselect(int from)
        {
            winFrom = from;
            InitializeComponent();
        }
        int winFrom = 1;
        private void kehuselect_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            LoadData();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        #region pager
        int pageSize = 20;
        int CurrentPage = 1;
        int pageCount = 0;
        DataTable dt;
        //int CurrentRowIndex = 0;
        //int CurrentColumnIndex = 0;
        void LoadData()
        {
            string where = "";
            if (txtKeyword.TextLength > 0)
                where = " name like'%" + txtKeyword.Text + "%'";
            int RowCount = db.count("users", where);
            dt = db.GetPagerData("users", "id,xingming,haoma,chepai", where, pageSize, CurrentPage);
            pageCount = (int)Math.Ceiling(RowCount * 1.0 / pageSize);
            lblPageCount.Text = "共 " + pageCount + " 页";
            txtPageCurrent.Text = CurrentPage.ToString();
            dataGridView1.DataSource = dt;
        }
        private void lblfirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 1;
            LoadData();
        }

        private void lblprev_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            if (CurrentPage < 1)
                CurrentPage = 1;
            LoadData();
        }

        private void lblNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            if (CurrentPage > pageCount)
                CurrentPage = pageCount;
            LoadData();
        }

        private void lbllast_Click(object sender, EventArgs e)
        {
            CurrentPage = pageCount;
            LoadData();
        }
        private void btnPageGo_Click(object sender, EventArgs e)
        {
            CurrentPage = int.Parse(txtPageCurrent.Text);
            LoadData();
        }
        #endregion
        private void button2_Click(object sender, EventArgs e)
        {
            int index = dataGridView1.CurrentRow.Index;
            if (index==-1)
            {
                MessageBox.Show("请选择客户");
            }
            else
            {
                if (winFrom == 1)
                {
                    var userId = dataGridView1.Rows[index].Cells[1].Value.ToString();
                    var name = dataGridView1.Rows[index].Cells[2].Value.ToString();
                    var tel = dataGridView1.Rows[index].Cells[3].Value.ToString();
                    var chepai = dataGridView1.Rows[index].Cells[4].Value.ToString();
                    string[] users = new string[]{ userId, name, tel , chepai };
                    weixiudan owner;
                    owner = (weixiudan)this.Owner;                    
                    owner.setUserinfo(users);
                    this.Close();
                }
                else if (winFrom == 2)
                {
                    //weixiudan owner;
                    //owner = (weixiudan)this.Owner;
                    //string goodsId = string.Join(",", ids.ToArray());
                    //owner.addGoodsToDataView(goodsId);
                    //this.Close();
                }

            }
        }

       
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                if ((bool)dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue == true)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = false;
                }
                else
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = true;
                }
            }
        }
    }
}
